<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<!-- Apple devices fullscreen -->
<meta name="apple-mobile-web-app-capable" content="yes">
<!-- Apple devices fullscreen -->
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<link href="/public/css/main.css" rel="stylesheet" type="text/css">
<link href="/public/css/page.css" rel="stylesheet" type="text/css">
<link href="/public/font/css/font-awesome.min.css" rel="stylesheet" />
<link href="/public/js/jquery-ui/jquery-ui.min.css" rel="stylesheet" type="text/css"/>
<link href="/public/js/perfect-scrollbar.min.css" rel="stylesheet" type="text/css"/>
<style type="text/css">html, body { overflow: visible;}</style>
<script type="text/javascript" src="/public/js/jquery.js"></script>
<script type="text/javascript" src="/public/js/jquery-ui/jquery-ui.min.js"></script>
<script type="text/javascript" src="/public/js/layer/layer.js"></script><!-- 弹窗js 参考文档 http://layer.layui.com/-->
<script type="text/javascript" src="/public/js/admin.js"></script>
<script type="text/javascript" src="/public/js/jquery.validation.min.js"></script>
<script type="text/javascript" src="/public/js/common.js"></script>
<script type="text/javascript" src="/public/js/perfect-scrollbar.min.js"></script>
<script type="text/javascript" src="/public/js/jquery.mousewheel.js"></script>

 <script type="text/javascript" charset="utf-8" src="/public/editor/ueditor.config.js"></script>
<script type="text/javascript" charset="utf-8" src="/public/editor/ueditor.all.min.js"> </script>
<script type="text/javascript" charset="utf-8" src="/public/editor/lang/zh-cn/zh-cn.js"></script>

<body style="background-color: #FFF; overflow: auto;">
<div id="toolTipLayer"
     style="position: absolute; z-index: 9999; display: none; visibility: visible; left: 95px; top: 573px;"></div>
<div id="append_parent"></div>
<div id="ajaxwaitid"></div>
<div class="page">
    <form class="form-horizontal" id="adminHandle" action="" method="post">
        <input type="hidden" name="type" value="{$type}">
        <div class="ncap-form-default">
            <div id="cons">
                <dl class="row">
                <dt class="tit">
                    <label for="password"><em></em>HTTP</label>
                </dt>
                <dd class="opt">
                    <input type="radio" name="http"  value="1"> 线上<input type="text" style="width: 200px" value="https://m.xiaomachuxing.cn">
                    <input type="radio" name="http"  value="2"> 线下<input type="text" style="width: 200px" value="http://192.168.1.144:8018">
                </dd>
                </dl>
                <dl class="row">
                <dt class="tit">
                    <label for="password"><em></em>标题</label>
                </dt>
                <dd class="opt">
                    <input type="text" name="title" id="title" value="" >
                </dd>
                </dl>
                <dl class="row">
                    <dt class="tit">
                        <label for="text"><em></em>接口管理</label>
                    </dt>
                    <dd class="opt">
                        <input type="text" style="width:400px ! important" value="" name="url" placeholder="URL" id="url" >
                        <a type="button" class="ncap-btn-big ncap-btn-success" id="adds">添加</a>
                        <a type="button" class="ncap-btn-big ncap-btn-success" id="addsfile">添加file</a>
                        <a type="button" class="ncap-btn-big ncap-btn-green" id="send">发送</a>
                        <a type="button" class="ncap-btn-big ncap-btn-orange" id="submits">保存</a>
                    </dd>
                </dl>
             
                    <dl class="row">
                        <dt class="tit">
                            <label for="text"></label>
                        </dt>
                        <dd class="opt">
                            <input style="width:100px ! important" value="token" type="text" name="name[]" class="aaa input-txt" placeholder="名称">---
                            <input style="width:100px ! important" value="" type="text" name="val[]" class="bbb input-txt" placeholder="值">--
                            <input style="width:100px ! important" value="" type="text" name="bak[]" class="input-txt" placeholder="备注">
                        </dd>
                    </dl>
                  
            </div>
            <dl class="row">
                <dt class="tit">
                    <label for="password"><em></em>模式</label>
                </dt>
                <dd class="opt">
                    <input type="radio" name="types"  value="1" checked="checked" > 常规模式
                    <input type="radio" name="types"  value="2"> 调试模式
                </dd>
                </dl>
            <dl class="row">
                <dt class="tit">
                    <label for="password"><em></em>结果</label>
                </dt>
                <dd class="opt">
                    <script name="result" id="editor" type="text/plain" style="width:960px;height:300px;"></script>
            
                </dd>
            </dl>
        </div>
    </form>
</div>
<script type="text/javascript">
    var http = '';
    $("input[name='http']").click(function(){
        http = $(this).next().val();
        $.post('/index/index/http',{'http':http},function(data){
      
        })
        
    });

    $(document).ready(function () {
        $(document).on('click', '#adds', function () {
            var str = ' <dl class="row">                    <dt class="tit">                     <label for="text"></label>                    </dt>                    <dd class="opt">                        <input type="text" name="name[]" class="aaa input-txt"  placeholder="名称" style="width:100px ! important" >---<input style="width:100px ! important" type="text" name="val[]" class="bbb input-txt"  placeholder="值">--  <input style="width:100px ! important" type="text" name="bak[]" class="input-txt"  placeholder="备注">                   </dd>                </dl>';
            $("#cons").append(str);
        });
        $(document).on('click', '#addsfile', function () {
            var str = ' <dl class="row">                    <dt class="tit">                     <label for="text"></label>                    </dt>                    <dd class="opt">                        <input type="text" name="name555[]" class="fileaaa input-txt"  placeholder="名称" style="width:100px ! important" >---<input style="width:100px ! important" type="file" class="filebbb input-txt"  placeholder="值">--  <input style="width:100px ! important" type="text" name="bak[]" class="input-txt"  placeholder="备注">                   </dd>                </dl>';
            $("#cons").append(str);
        });
    });

    //实例化编辑器
    //建议使用工厂方法getEditor创建和引用编辑器实例，如果在某个闭包下引用该编辑器，直接调用UE.getEditor('editor')就能拿到相关的实例
    var ue = UE.getEditor('editor');
    $(document).on('click', '#send', function(){
        if(http == ''){
            alert('未选择HTTP模式');
            return;
        }
        var str = '';
       $(".aaa").each(function(index){
        var k = $('.aaa').eq(index).val();
        var v = $('.bbb').eq(index).val();
        if(k != ''){
            str += k+"="+v+"&";
        }
       });
       str=str.substring(0,str.length-1);
       var url = $("#url").val();
       if(url == ''){
            alert('请填写接口url');
            return;
       }
       var type = $("input[name='types']:checked").val();

       var app_type = {$type};
       var filevalue;
       //*****判断是否上传图片
       filevalue = $('.fileaaa').eq(0).val();
  
       if((filevalue != '') && (typeof (filevalue) != "undefined")){
    
            var formFile = new FormData();
            $(".fileaaa").each(function(index){
                var fileObj = $(".filebbb")[index].files[0];
                // js 获取文件对象
                if (typeof (fileObj) == "undefined" || fileObj.size <= 0) {
                   alert("请选择图片");
                   return;
                }
                var kaaa = $('.fileaaa').eq(index).val();
                formFile.append(kaaa, fileObj); //加入文件对象
           });
            var index2 = layer.load(2);
            $.post('/index/index/sec',{"str":str,"app_type":app_type},function(datas){
                if(datas.code){
                    formFile.append("encode",datas.code);
                    var data = formFile;
                    $.ajax({
                       url: http + url,
                       data: data,
                       type: "Post",
                       dataType: "json",
                       cache: false,//上传文件无需缓存
                       processData: false,//用于对data参数进行序列化处理 这里必须false
                       contentType: false, //必须
                       success: function (result) {
                        $.post('/index/index/dec',{"mistr":result,"app_type":app_type},function(res){
                            layer.close(index2); 
                                console.log(res)
                                setContent(res);
                        })

                       },
                    })
               }
           },'json')
            

       }else{
         
            if(type == 2){
                str = encodeURIComponent(str);
                window.open("{:url('/index/index/test','url=')}"+url+"&str="+str+"&type="+type+"&app_type="+app_type);
            }else{
                var index1 = layer.load(2);
                $.get("/index/index/test",{"url":url,"str":str,"type":type,"app_type":app_type},function(data){
                    layer.close(index1); 
                    setContent(data);
                })
            }
       }
       
        
    })
    $(document).on('click', '#submits', function(){
        var str = '';
        var title = $("#title").val();
        if(title == ''){
            alert('标题不能为空！');            
            return false;
        }
        $('form').submit();
    })

 
    function setblur(e){
        UE.getEditor('editor').blur();
        UE.dom.domUtils.preventDefault(e)
    }
    function insertHtml() {
        var value = prompt('插入html代码', '');
        UE.getEditor('editor').execCommand('insertHtml', value)
    }
    function createEditor() {
        enableBtn();
        UE.getEditor('editor');
    }
    function getAllHtml() {
        alert(UE.getEditor('editor').getAllHtml())
    }
    function getContent() {
        var arr = [];
        arr.push("使用editor.getContent()方法可以获得编辑器的内容");
        arr.push("内容为：");
        arr.push(UE.getEditor('editor').getContent());
        alert(arr.join("\n"));
    }
    function getPlainTxt() {
        var arr = [];
        arr.push("使用editor.getPlainTxt()方法可以获得编辑器的带格式的纯文本内容");
        arr.push("内容为：");
        arr.push(UE.getEditor('editor').getPlainTxt());
        alert(arr.join('\n'))
    }
    function setContent(data) {
        UE.getEditor('editor').setContent(data);
    }
    function setDisabled() {
        UE.getEditor('editor').setDisabled('fullscreen');
        disableBtn("enable");
    }

    function setEnabled() {
        UE.getEditor('editor').setEnabled();
        enableBtn();
    }

    function getText() {
        //当你点击按钮时编辑区域已经失去了焦点，如果直接用getText将不会得到内容，所以要在选回来，然后取得内容
        var range = UE.getEditor('editor').selection.getRange();
        range.select();
        var txt = UE.getEditor('editor').selection.getText();
        alert(txt)
    }

    function getContentTxt() {
        var arr = [];
        arr.push("使用editor.getContentTxt()方法可以获得编辑器的纯文本内容");
        arr.push("编辑器的纯文本内容为：");
        arr.push(UE.getEditor('editor').getContentTxt());
        alert(arr.join("\n"));
    }
    function hasContent() {
        var arr = [];
        arr.push("使用editor.hasContents()方法判断编辑器里是否有内容");
        arr.push("判断结果为：");
        arr.push(UE.getEditor('editor').hasContents());
        alert(arr.join("\n"));
    }
    function setFocus() {
        UE.getEditor('editor').focus();
    }
    function deleteEditor() {
        disableBtn();
        UE.getEditor('editor').destroy();
    }
    function disableBtn(str) {
        var div = document.getElementById('btns');
        var btns = UE.dom.domUtils.getElementsByTagName(div, "button");
        for (var i = 0, btn; btn = btns[i++];) {
            if (btn.id == str) {
                UE.dom.domUtils.removeAttributes(btn, ["disabled"]);
            } else {
                btn.setAttribute("disabled", "true");
            }
        }
    }
    function enableBtn() {
        var div = document.getElementById('btns');
        var btns = UE.dom.domUtils.getElementsByTagName(div, "button");
        for (var i = 0, btn; btn = btns[i++];) {
            UE.dom.domUtils.removeAttributes(btn, ["disabled"]);
        }
    }

    function getLocalData () {
        alert(UE.getEditor('editor').execCommand( "getlocaldata" ));
    }

    function clearLocalData () {
        UE.getEditor('editor').execCommand( "clearlocaldata" );
        alert("已清空草稿箱")
    }

</script>
</body>
</html>